package com.bawei.router

import android.content.Context
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Interceptor
import com.alibaba.android.arouter.facade.callback.InterceptorCallback
import com.alibaba.android.arouter.facade.template.IInterceptor
import com.bawei.base.path.RoutePath
import com.tencent.mmkv.MMKV

/**
 * Author: Jason Zhang
 * Date: 2025/4/24
 * Desc: Token路由拦截器
 */

@Interceptor(priority = 4, name = "路由Token拦截器")
class RouteTokenInterceptor : IInterceptor{
    private lateinit var mContext: Context

    override fun init(context: Context?) {
        if (context != null){
            mContext = context
        }
    }

    override fun process(postcard: Postcard, callback: InterceptorCallback) {
        val loginToken = MMKV.defaultMMKV().decodeString("token", "31312321")
        if (loginToken.isNullOrEmpty()) {
            postcard.group = RoutePath.GROUP_LOGIN //必须指定路由组
            postcard.path = RoutePath.PATH_LOGIN_PAGE
            postcard.navigation()
        } else {
            callback.onContinue(postcard)
        }
    }


}